package ufabc.edu.helppo.rpc.command.impl;

import static ufabc.edu.helppo.rpc.RPCOpenSimMethods.ADMIN_LOAD_OAR;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.FILE_NAME;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.REGION_NAME;
import static ufabc.edu.helppo.rpc.RPCOpenSimParameters.SERVER_PASSWORD;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.client.XmlRpcClient;

import ufabc.edu.helppo.gerador.exceptions.NenhumEspacoDisponivelException;
import ufabc.edu.helppo.rpc.command.RPCCommand;
import ufabc.edu.helppo.rpc.singleton.HelppoProperties;
import ufabc.edu.helppo.rpc.singleton.RpcClientSingleton;

public class LoadOarRPCCommandImpl extends RPCCommand {
	
	private String filename;
	private String nomeRegiao;
	
	@Override
	public boolean execute() throws MalformedURLException, XmlRpcException,
			IOException, NenhumEspacoDisponivelException {
		XmlRpcClient client = RpcClientSingleton.getInstance();
		configure(client);
		HashMap<String, String> retorno = (HashMap<String, String>)client.execute(ADMIN_LOAD_OAR, parameter());
		
		return true;//Boolean.parseBoolean(retorno.get("success"));
	}

	private List<Hashtable<String, Object>> parameter() throws IOException,
			NenhumEspacoDisponivelException {
		Hashtable<String, Object> parameters = new Hashtable<String, Object>();

		ResourceBundle properties = HelppoProperties.getInstance();

		parameters.put(FILE_NAME, filename);
		parameters.put(REGION_NAME, nomeRegiao);
		parameters.put(SERVER_PASSWORD, properties.getString(SERVER_PASSWORD));

		List<Hashtable<String, Object>> list = new ArrayList<Hashtable<String, Object>>();
		list.add(parameters);

		return list;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public void setNomeRegiao(String nomeRegiao_) {
		nomeRegiao = nomeRegiao_;
	}
}
